// pages/Payment/phone.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '手机验证', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.setData({
    //   apply_id:options.apply_id,
    //   orderid:options.orderid,
    //   bank_type:options.bank_type,
    //   tel:options.tel,
    //   card_id:options.card_id,
    //   type:options.type,
    //   add:options.add,
    // })
    console.log(options)
    var _this = this;
    _this.setData({ upType: options.upType })
    if (options.add == 1) {
      var tel = options.tel;
      var phone = tel.substring(0, 3) + '*****' + tel.substring(7, 11)
      _this.setData({
        phone: phone,
        orderid: options.orderid,
        apply_id: options.apply_id,
        add: options.add
      })


    } else {
      //支付过来
      _this.setData({
        phone: options.mobile,
        orderid: options.orderid,
        add: options.add
      })


    }
    //获取添加银行卡的验证码
    var time = 60;
    _this.setData({
      codeStatus: 2,
      codeText: '60s后重新获取'
    })
    var codeTime = setInterval(function () {
      if (time == 0) {
        clearInterval(codeTime)
        _this.setData({
          codeStatus: 1,
          codeText: '重新获取'
        })
      } else {
        _this.setData({
          codeText: time + 's后重新获取'
        })
        time--;
      }
    }, 1000)
  },
  //获取验证码
  onCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  //重新获取验证码
  onCodeTime() {
    var _this = this;
    console.log(_this.data.codeStatus, _this.data.add)
    if (_this.data.codeStatus == 1) {
      if (_this.data.add == 1) {
        $api.bankbankremsg({
          login_token: wx.getStorageSync('login_token'),//用户登录token
          apply_id: _this.data.apply_id//第一步的申请id
        }).then((res) => {
          if (res.data.code == 0) {
            wx.showToast({
              title: '发送成功',
              icon: 'none',
              duration: 2000
            })
            var time = 60;
            _this.setData({
              codeStatus: 2,
              codeText: '60s后重新获取'
            })
            var codeTime = setInterval(function () {
              if (time == 0) {
                clearInterval(codeTime)
                _this.setData({
                  codeStatus: 1,
                  codeText: '重新获取'
                })
              } else {
                _this.setData({
                  codeText: time + 's后重新获取'
                })
                time--;
              }
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      } else if (_this.data.add == 2) {
        $api.bankpayremsg({
          login_token: wx.getStorageSync('login_token'),//用户登录token
          order_id: _this.data.orderid//第一步的申请id
        }).then((res) => {
          if (res.data.code == 0) {
            wx.showToast({
              title: '发送成功',
              icon: 'none',
              duration: 2000
            })
            var time = 60;
            _this.setData({
              codeStatus: 2,
              codeText: '60s后重新获取'
            })
            var codeTime = setInterval(function () {
              if (time == 0) {
                clearInterval(codeTime)
                _this.setData({
                  codeStatus: 1,
                  codeText: '重新获取'
                })
              } else {
                _this.setData({
                  codeText: time + 's后重新获取'
                })
                time--;
              }
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    }
  },
  //提交
  onDetermine() {
    var _this = this;
    if (_this.data.code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({ loadModal: true })
      if (_this.data.add == 1) {
        $api.confirmUserBank({
          login_token: wx.getStorageSync('login_token'),//用户登录token
          apply_id: _this.data.apply_id,//第一步的申请id
          sms_code: _this.data.code//短信验证码
        }).then((res) => {
          if (res.data.code == 0) {
            _this.setData({ loadModal: false })
            wx.showToast({
              title: '添加成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/Payment/Payment?order_id=' + _this.data.orderid + '&upType=' + _this.data.upType,
              })
            }, 2000)
          } else {
            _this.setData({ loadModal: false })
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      } else if (_this.data.add == 2) {
        $api.bankpayconfirm({
          login_token: wx.getStorageSync('login_token'),//用户登录token
          order_id: _this.data.orderid,//订单id
          sms_code: _this.data.code//短信验证码
        }).then((res) => {
          if (res.data.code == 0) {
            _this.setData({ loadModal: false })
            wx.showToast({
              title: '支付确认成功',
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/order/order?type=0',
              })
            }, 2000)
          } else {
            _this.setData({ loadModal: false })
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    }
  }
})